package com.taxshare.seller.util;

import cn.hutool.core.date.DateUtil;

import cn.hutool.core.util.StrUtil;
import com.taxshare.seller.entity.SellerInvoiceDetailEntity;
import com.taxshare.seller.entity.SellerInvoiceEntity;
import com.taxshare.tools.util.AreaCodeUtil;
import com.taxshare.tools.util.BeanSelfUtils;
import com.taxshare.tools.util.DoubleUtils;
import com.taxshare.tools.util.StringUtil;
import lombok.extern.slf4j.Slf4j;


import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.URL;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@Slf4j
public class InvoiceImgUtil {

    public BufferedImage makeInvoiceImage(SellerInvoiceEntity sellerInvoiceEntity, List<SellerInvoiceDetailEntity> sellerInvoiceDetailEntityList, URL templateUrl) throws IOException {
        BufferedImage image = javax.imageio.ImageIO.read(templateUrl);
        Graphics graphics = image.getGraphics();
        //发票代码（134,50）
        graphics.setColor(new Color(181, 176, 166));//颜色
        graphics.setFont(new Font("哥特字体", Font.PLAIN, 24));
        String invoiceCode = sellerInvoiceEntity.getInvoiceCode();
        graphics.drawString(invoiceCode == null ? "" : invoiceCode, 134, 50);

        if("12".equals(sellerInvoiceEntity.getInvoiceType())){
            graphics.setColor(Color.BLACK);
            graphics.setFont(new Font("哥特字体",  Font.BOLD, 14));
            graphics.drawString("机动车", 230, 90);
        }

        //校验码（134,50）
        graphics.setColor(new Color(118, 137, 170));//颜色
        graphics.setFont(new Font("哥特字体", Font.PLAIN, 12));

        //抬头（330,66）
        graphics.setColor(new Color(167, 116, 90));//颜色
        graphics.setFont(new Font("哥特字体", Font.BOLD, 16));//字体
        String area2 = "";
        String invoiceType = sellerInvoiceEntity.getInvoiceType();

        String fplx = getFplxStr(invoiceCode, invoiceType);
        graphics.drawString(fplx, 370, 43);

        //印章中心（390,66）
        graphics.setColor(Color.RED);
        graphics.setFont(new Font("哥特字体", Font.PLAIN, 14));
        graphics.drawString(area2, 410, 66);

        //发票号码（612,50）
        graphics.setColor(Color.blue);
        graphics.setFont(new Font("哥特字体", Font.PLAIN, 24));
        String invoiceNo = sellerInvoiceEntity.getInvoiceNo();
        graphics.drawString(invoiceNo == null ? "" : invoiceNo, 610, 50);

        //小字体的发票代码
        graphics.setColor(Color.blue);
        graphics.setFont(new Font("哥特字体", Font.PLAIN, 12));
        graphics.drawString(invoiceCode == null ? "" : invoiceCode, 736, 50);

        //小字体的发票号码
        graphics.setColor(Color.blue);
        graphics.setFont(new Font("哥特字体", Font.PLAIN, 18));
        graphics.drawString(invoiceNo == null ? "" : invoiceNo, 736, 70);

        //开票日期（660，90）
        graphics.setColor(Color.blue);
        graphics.setFont(new Font("哥特字体", Font.PLAIN, 12));
        String invoiceDate = sellerInvoiceEntity.getInvoiceDate();
        if (StrUtil.isNotBlank(invoiceDate)) {
            graphics.drawString(invoiceDate == null ? "" : transAddLine(invoiceDate).substring(0, 4) + "年"
                    + transAddLine(invoiceDate).substring(5, 7) + "月" + transAddLine(invoiceDate).substring(8, 10) + "日", 680, 86);
        } else {
            graphics.drawString("", 680, 86);
        }


        //购方名称(164,123)
        graphics.setColor(Color.blue);
        graphics.setFont(new Font("哥特字体", Font.PLAIN, 12));
        String purchaserName = sellerInvoiceEntity.getPurchaserName();
        graphics.drawString(purchaserName == null ? "" : purchaserName, 168, 119);

        //购方税号(164,144)
        graphics.setColor(Color.blue);
        graphics.setFont(new Font("哥特字体", Font.PLAIN, 12));
        String purchaserTaxNo = sellerInvoiceEntity.getPurchaserTaxNo();
        graphics.drawString(purchaserTaxNo == null ? "" : purchaserTaxNo, 168, 138);

        //地址、电话(164,162)GFDZDH
        graphics.setColor(Color.blue);
        graphics.setFont(new Font("哥特字体", Font.PLAIN, 12));
        String purchaserAddressTel = StringUtil.validator(sellerInvoiceEntity.getPurchaserAddressTel());
        if (purchaserAddressTel.length() > 35) {
            graphics.setFont(new Font("哥特字体", Font.PLAIN, 10));
        }
        graphics.drawString(purchaserAddressTel, 168, 157);

        //开户行及账号(164,186)GFYHZH
        graphics.setColor(Color.blue);
        graphics.setFont(new Font("哥特字体", Font.PLAIN, 12));
        String purchaserBankAccount = StringUtil.validator(sellerInvoiceEntity.getPurchaserBankAccount());

        if (purchaserBankAccount.length() > 35) {
            graphics.setFont(new Font("哥特字体", Font.PLAIN, 10));
        }
        graphics.drawString(purchaserBankAccount, 168, 177);

        //
        //判断有没有密文和密文长度：MWQ
        String cipherText = sellerInvoiceEntity.getCipherText();
        graphics = getITEXT(graphics, cipherText);

        //明细（）
        graphics = getDetail(graphics, sellerInvoiceDetailEntityList);
        //未税金额合计538,347
        graphics = getInvoiceOne(graphics, sellerInvoiceEntity);

        //收款人
        graphics.setColor(Color.blue);
        graphics.setFont(new Font("哥特字体", Font.PLAIN, 12));
        String payee = sellerInvoiceEntity.getPayee();
        graphics.drawString(payee == null ? "" : payee, 130, 470);

        //复核
        graphics.setColor(Color.blue);
        graphics.setFont(new Font("哥特字体", Font.PLAIN, 12));
        String reviewer = sellerInvoiceEntity.getReviewer();
        graphics.drawString(reviewer == null ? "" : reviewer, 330, 470);

        //开票人
        graphics.setColor(Color.blue);
        graphics.setFont(new Font("哥特字体", Font.PLAIN, 12));
        String drawer = sellerInvoiceEntity.getDrawer();
        graphics.drawString(drawer == null ? "" : drawer, 505, 470);

        return image;
    }

    public BufferedImage makeCarInvoiceImage(SellerInvoiceEntity sellerInvoiceEntity, List<SellerInvoiceDetailEntity> sellerInvoiceDetailEntityList, URL templateUrl) throws IOException {
        BufferedImage image = javax.imageio.ImageIO.read(templateUrl);
        Graphics graphics = image.getGraphics();
        //机打代码（212,197）
        graphics.setColor(new Color(181, 176, 166));//颜色
        graphics.setFont(new Font("哥特字体", Font.PLAIN, 24));
        String machinInvoiceCode = sellerInvoiceEntity.getInvoiceCode();
        graphics.drawString(machinInvoiceCode == null ? "" : machinInvoiceCode, 212, 197);

        //机打号码（212,229）
        graphics.setColor(new Color(181, 176, 166));//颜色
        graphics.setFont(new Font("哥特字体", Font.PLAIN, 24));
        String machinInvoiceNo = sellerInvoiceEntity.getInvoiceNo();
        graphics.drawString(machinInvoiceNo == null ? "" : machinInvoiceNo, 212, 229);

        //机器编号（212,260）
        graphics.setColor(new Color(181, 176, 166));
        graphics.setFont(new Font("哥特字体", Font.PLAIN, 24));
        String machinNo = sellerInvoiceDetailEntityList.get(0).getMachinNo();
        graphics.drawString(machinNo == null ? "" : machinNo, 212, 260);

        //购方名称（212，315）
        graphics.setColor(Color.blue);
        graphics.setFont(new Font("哥特字体", Font.PLAIN, 15));
        String purchaserName = sellerInvoiceEntity.getPurchaserName();
        graphics.drawString(purchaserName == null ? "" : purchaserName, 212, 298);

        //组织机构代码（212，298）
        graphics.setColor(Color.blue);
        graphics.setFont(new Font("哥特字体", Font.PLAIN, 15));
        String purchaserTaxNo = sellerInvoiceEntity.getPurchaserTaxNo();
        graphics.drawString(purchaserTaxNo == null ? "" : purchaserTaxNo, 212, 315);

        //车辆类型(212,315)
        graphics.setColor(Color.blue);
        graphics.setFont(new Font("哥特字体", Font.PLAIN, 15));
        String carType = sellerInvoiceDetailEntityList.get(0).getCarCategoryName();
        graphics.drawString(carType == null ? "" : carType, 212, 353);

        //合格证号(212,393)
        graphics.setColor(Color.blue);
        graphics.setFont(new Font("哥特字体", Font.PLAIN, 15));
        String certificateNum = sellerInvoiceDetailEntityList.get(0).getCertificateNum();
        graphics.drawString(certificateNum == null ? "" : certificateNum, 212, 393);

        //发动机号码(212，431)
        graphics.setColor(Color.blue);
        graphics.setFont(new Font("哥特字体", Font.PLAIN, 15));
        String engineNum = sellerInvoiceDetailEntityList.get(0).getEngineNum();
        graphics.drawString(engineNum == null ? "" : engineNum, 212, 431);

        //发票代码777，109
        graphics.setColor(Color.blue);
        graphics.setFont(new Font("哥特字体", Font.PLAIN, 15));
        String invoiceCode = sellerInvoiceEntity.getInvoiceCode();
        graphics.drawString(invoiceCode == null ? "" : invoiceCode, 777, 109);

        //发票号码777，137
        graphics.setColor(Color.blue);
        graphics.setFont(new Font("哥特字体", Font.PLAIN, 15));
        String invoiceNo = sellerInvoiceEntity.getInvoiceNo();
        graphics.drawString(invoiceNo == null ? "" : invoiceNo, 777, 137);

//        String fplx = getFplxStr(invoiceCode, invoiceType);
//        graphics.drawString(fplx, 370, 43);

        //小字体的发票代码(777，109)
        graphics.setColor(Color.blue);
        graphics.setFont(new Font("哥特字体", Font.PLAIN, 15));
        graphics.drawString(invoiceCode == null ? "" : invoiceCode, 777, 109);

        //小字体的发票号码777，137
        graphics.setColor(Color.blue);
        graphics.setFont(new Font("哥特字体", Font.PLAIN, 15));
        graphics.drawString(invoiceNo == null ? "" : invoiceNo, 777, 137);

//        //价税合计(212，471)
//        graphics.setColor(Color.blue);
//        graphics.setFont(new Font("哥特字体", Font.PLAIN, 12));
//        String totalSumTax = sellerInvoiceEntity.getTotalSumTax();
//        graphics.drawString(totalSumTax == null ? "" : totalSumTax, 212, 471);

        //销货单位名称(212，507)
        graphics.setColor(Color.blue);
        graphics.setFont(new Font("哥特字体", Font.PLAIN, 15));
        String sellerName = sellerInvoiceEntity.getSellerName();
        graphics.drawString(sellerName == null ? "" : sellerName, 212, 507);

        //纳税人识别号(212，543)
        graphics.setColor(Color.blue);
        graphics.setFont(new Font("哥特字体", Font.PLAIN, 15));
        String sellerTaxNo = sellerInvoiceEntity.getSellerTaxNo();
        graphics.drawString(sellerTaxNo == null ? "" : sellerTaxNo, 212, 543);

        //地址(212，581)
        graphics.setColor(Color.blue);
        graphics.setFont(new Font("哥特字体", Font.PLAIN, 15));
        String sellerAddress = sellerInvoiceEntity.getSellerAddress();
        graphics.drawString(sellerAddress == null ? "" : sellerAddress, 212, 581);

        //增值税税率或征收率(212，633)
        graphics.setColor(Color.blue);
        graphics.setFont(new Font("哥特字体", Font.PLAIN, 15));
        String taxRate = sellerInvoiceEntity.getTaxRate();
        graphics.drawString(taxRate == null ? "" : taxRate, 212, 633);

        //纳税人识别号666,316
        graphics.setColor(Color.blue);
        graphics.setFont(new Font("哥特字体", Font.PLAIN, 15));
        String buyerTaxNo = sellerInvoiceEntity.getPurchaserTaxNo();
        graphics.drawString(buyerTaxNo == null ? "" : buyerTaxNo, 666, 316);

        //厂牌型号(453,352)
        graphics.setColor(Color.blue);
        graphics.setFont(new Font("哥特字体", Font.PLAIN, 15));
        String brandMode = sellerInvoiceDetailEntityList.get(0).getBrandMode();
        graphics.drawString(brandMode == null ? "" : brandMode, 453, 352);

        //产地(812，354)
        graphics.setColor(Color.blue);
        graphics.setFont(new Font("哥特字体", Font.PLAIN, 15));
        String productionPlaceName = sellerInvoiceDetailEntityList.get(0).getProductionPlaceName();
        graphics.drawString(productionPlaceName == null ? "" : productionPlaceName, 812, 354);

        //进口证明书好482,394
        graphics.setColor(Color.blue);
        graphics.setFont(new Font("哥特字体", Font.PLAIN, 15));
        String importTestimonialNum = sellerInvoiceDetailEntityList.get(0).getImportTestimonialNum();
        graphics.drawString(importTestimonialNum == null ? "" : importTestimonialNum, 482, 394);

        //商检单号811,394
        graphics.setColor(Color.blue);
        graphics.setFont(new Font("哥特字体", Font.PLAIN, 15));
        String inspectionOdd = sellerInvoiceDetailEntityList.get(0).getInspectionOdd();
        graphics.drawString(inspectionOdd == null ? "" : inspectionOdd, 811, 394);

        //车辆识别号/车架号码647，431
        graphics.setColor(Color.blue);
        graphics.setFont(new Font("哥特字体", Font.PLAIN, 15));
        String carDiscernCode = sellerInvoiceDetailEntityList.get(0).getCarDiscernCode();
        graphics.drawString(carDiscernCode == null ? "" : carDiscernCode, 647, 431);

//        //价税合计小写748,471
//        graphics.setColor(Color.blue);
//        graphics.setFont(new Font("哥特字体", Font.PLAIN, 12));
//        String totalSumTax1 = sellerInvoiceEntity.getTotalSumTax();
//        graphics.drawString(totalSumTax1 == null ? "" : totalSumTax1, 748, 471);

        //电话673,507
        graphics.setColor(Color.blue);
        graphics.setFont(new Font("哥特字体", Font.PLAIN, 15));
        String sellerPhone = sellerInvoiceDetailEntityList.get(0).getTelephone();
        graphics.drawString(sellerPhone == null ? "" : sellerPhone, 673, 507);

        //账号673，543
        graphics.setColor(Color.blue);
        graphics.setFont(new Font("哥特字体", Font.PLAIN, 15));
        String sellerBankAccount = sellerInvoiceEntity.getSellerBankAccount();
        graphics.drawString(sellerBankAccount == null ? "" : sellerBankAccount, 673, 543);

        //开户银行575,580
        graphics.setColor(Color.blue);
        graphics.setFont(new Font("哥特字体", Font.PLAIN, 15));
        String sellerDepositBank = sellerInvoiceEntity.getSellerDepositBank();
        graphics.drawString(sellerDepositBank == null ? "" : sellerDepositBank, 575, 580);

//        //增值税税额389，633
//        graphics.setColor(Color.blue);
//        graphics.setFont(new Font("哥特字体", Font.PLAIN, 12));
//        String totalTax = sellerInvoiceEntity.getTotalTax();
//        graphics.drawString(totalTax == null ? "" : totalTax, 389, 633);

        //主管税务机关及名称628，610
        graphics.setColor(Color.blue);
        graphics.setFont(new Font("哥特字体", Font.PLAIN, 14));
        String taxOfName = sellerInvoiceDetailEntityList.get(0).getTaxOfName();
        graphics.drawString(taxOfName == null ? "" : taxOfName, 628, 610);

        //主管税务机关代码628，631
        graphics.setColor(Color.blue);
        graphics.setFont(new Font("哥特字体", Font.PLAIN, 15));
        String taxOfCode = sellerInvoiceDetailEntityList.get(0).getTaxOfCode();
        graphics.drawString(taxOfCode == null ? "" : taxOfCode, 628, 631);

        //不含税价小写251，668
        graphics.setColor(Color.blue);
        graphics.setFont(new Font("哥特字体", Font.PLAIN, 15));
        String totalSum = sellerInvoiceEntity.getTotalSum();
        graphics.drawString(totalSum == null ? "" : totalSum, 251, 668);

        //完税凭证号码513，668
        graphics.setColor(Color.blue);
        graphics.setFont(new Font("哥特字体", Font.PLAIN, 15));
        String taxVoucherNo = sellerInvoiceEntity.getTaxVoucherNo();
        graphics.drawString(taxVoucherNo == null ? "" : taxVoucherNo, 513, 668);

        //吨位711，668
        graphics.setColor(Color.blue);
        graphics.setFont(new Font("哥特字体", Font.PLAIN, 15));
        String tonnage = sellerInvoiceDetailEntityList.get(0).getTonnage();
        graphics.drawString(tonnage == null ? "" : tonnage, 711, 668);

        //限乘人数843，668
        graphics.setColor(Color.blue);
        graphics.setFont(new Font("哥特字体", Font.PLAIN, 15));
        String maximumPeople = sellerInvoiceDetailEntityList.get(0).getMaximumPeople();
        graphics.drawString(maximumPeople == null ? "" : maximumPeople, 843, 668);

        //开票日期154,137
        graphics.setColor(Color.blue);
        graphics.setFont(new Font("哥特字体", Font.PLAIN, 15));
        String invoiceDate = sellerInvoiceEntity.getInvoiceDate();
        if (StrUtil.isNotBlank(invoiceDate)) {
            graphics.drawString(invoiceDate == null ? "" : transAddLine(invoiceDate).substring(0, 4) + "年"
                    + transAddLine(invoiceDate).substring(5, 7) + "月" + transAddLine(invoiceDate).substring(8, 10) + "日", 154, 137);
        } else {
            graphics.drawString("", 680, 86);
        }

        //
        //判断有没有密文和密文长度：MWQ
        String cipherText = sellerInvoiceEntity.getCipherText();
        graphics = getCarITEXT(graphics, cipherText);

        graphics = getCarInvoiceOne(graphics, sellerInvoiceEntity);

        //开票人473，706
        graphics.setColor(Color.blue);
        graphics.setFont(new Font("哥特字体", Font.PLAIN, 15));
        String drawer = sellerInvoiceEntity.getDrawer();
        graphics.drawString(drawer == null ? "" : drawer, 473, 706);

        return image;
    }

    /**
     * 根据指定宽度自动换行
     * @param g
     * @param maxWdith
     * @param strContent
     * @param loc_X
     * @param loc_Y
     * @param font
     */
    private static   void  drawStringWithFontStyleLineFeed(Graphics g, String strContent,int maxWdith, int loc_X, int loc_Y, Font font){
        g.setFont(font);
        //获取字符串 字符的总宽度
        int strWidth =getStringLength(g,strContent);
        //每一行字符串宽度
        int rowWidth=maxWdith;
        //获取字符高度
        int strHeight=getStringHeight(g);
        //字符串总个数
        if(strWidth>rowWidth){
            char[] strContentArr = strContent.toCharArray();
            int count = 0;
            int conut_value = 0;
            int line = 0;
            int charWidth = 0;
            for(int j=0;j< strContentArr.length;j++){
                if(conut_value>=rowWidth){
                    conut_value = 0;
                    g.drawString(strContent.substring(count,j),loc_X,loc_Y+strHeight*line);
                    count = j;
                    line++;
                }else{
                    if(j==strContentArr.length - 1){
                        g.drawString(strContent.substring(count,j),loc_X,loc_Y+strHeight*line);
                    }else{
                        charWidth = g.getFontMetrics().charWidth(strContentArr[j]);
                        conut_value = charWidth + conut_value;
                    }
                }
            }
        }else{
            //直接绘制
            g.drawString(strContent, loc_X, loc_Y);
        }

    }

    private static int  getStringLength(Graphics g,String str) {
        char[]  strcha=str.toCharArray();
        int strWidth = g.getFontMetrics().charsWidth(strcha, 0, str.length());
        return strWidth;
    }

    private static int  getStringHeight(Graphics g) {
        int height = g.getFontMetrics().getHeight();
        return height;
    }

    public static Graphics getInvoiceOne(Graphics graphics, SellerInvoiceEntity sellerInvoiceEntity) {
        graphics.setColor(Color.blue);
        graphics.setFont(new Font("哥特字体", Font.PLAIN, 12));
        String totalSum = sellerInvoiceEntity.getTotalSum();
        graphics.drawString("￥ " + String.valueOf(totalSum), 590, 347);

        //税额
        graphics.setColor(Color.blue);
        graphics.setFont(new Font("哥特字体", Font.PLAIN, 12));
        String totalTax = sellerInvoiceEntity.getTotalTax();
        graphics.drawString("￥ " + String.valueOf(totalTax), 735, 347);

        //价税合计（大写）
        String totalSumTax = sellerInvoiceEntity.getTotalSumTax();
        graphics.setColor(Color.blue);
        graphics.setFont(new Font("哥特字体", Font.PLAIN, 12));
        graphics.drawString(totalSumTax == null ? "" : BeanSelfUtils.change(DoubleUtils.stringFormatdouble(totalSumTax)), 280, 370);

        //价税合计小写
        graphics.setColor(Color.blue);
        graphics.setFont(new Font("哥特字体", Font.PLAIN, 12));
        graphics.drawString(totalSumTax == null ? "" : totalSumTax, 690, 370);

        //销方名称(164,123)
        graphics.setColor(Color.blue);
        graphics.setFont(new Font("哥特字体", Font.PLAIN, 12));
        String sellerName = sellerInvoiceEntity.getSellerName();
        graphics.drawString(sellerName == null ? "" : sellerName, 168, 395);

        //销方税号(164,144)
        graphics.setColor(Color.blue);
        graphics.setFont(new Font("哥特字体", Font.PLAIN, 12));
        String sellerTaxNo = sellerInvoiceEntity.getSellerTaxNo();
        graphics.drawString(sellerTaxNo == null ? "" : sellerTaxNo, 168, 412);

        //销地址、电话(164,162)
        graphics.setColor(Color.blue);
        graphics.setFont(new Font("哥特字体", Font.PLAIN, 12));
        String sellerAddressTel = StringUtil.validator(sellerInvoiceEntity.getSellerAddressTel());
        if (sellerAddressTel.length() > 35) {
            graphics.setFont(new Font("哥特字体", Font.PLAIN, 10));
        }
        graphics.drawString(sellerAddressTel, 168, 429);

        //销开户行及账号(164,186)
        graphics.setColor(Color.blue);
        graphics.setFont(new Font("哥特字体", Font.PLAIN, 12));
        String sellerBankAccount = StringUtil.validator(sellerInvoiceEntity.getSellerBankAccount());
        if (sellerBankAccount.length() > 35) {
            graphics.setFont(new Font("哥特字体", Font.PLAIN, 10));
        }
        graphics.drawString(sellerBankAccount, 168, 446);

        //备注invcomment
        graphics.setColor(Color.blue);
        graphics.setFont(new Font("哥特字体", Font.PLAIN, 12));
        String remark = sellerInvoiceEntity.getRemark();
        //graphics.drawString(remark == null ? "" : remark, 510, 405);
        drawStringWithFontStyleLineFeed(graphics,remark == null ? "" : remark,250,510,405,new Font("哥特字体", Font.PLAIN, 12));
        return graphics;
    }

    public static Graphics getCarInvoiceOne(Graphics graphics, SellerInvoiceEntity sellerInvoiceEntity) {
        graphics.setColor(Color.blue);
        graphics.setFont(new Font("哥特字体", Font.PLAIN, 15));
        String totalSum = sellerInvoiceEntity.getTotalSum();
        graphics.drawString("￥ " + String.valueOf(totalSum), 251, 668);

        //税额
        graphics.setColor(Color.blue);
        graphics.setFont(new Font("哥特字体", Font.PLAIN, 15));
        String totalTax = sellerInvoiceEntity.getTotalTax();
        graphics.drawString("￥ " + String.valueOf(totalTax), 389, 633);

        //价税合计（大写）
        String totalSumTax = sellerInvoiceEntity.getTotalSumTax();
        graphics.setColor(Color.blue);
        graphics.setFont(new Font("哥特字体", Font.PLAIN, 15));
        graphics.drawString(totalSumTax == null ? "" : BeanSelfUtils.change(DoubleUtils.stringFormatdouble(totalSumTax)), 212, 474);

        //价税合计小写
        graphics.setColor(Color.blue);
        graphics.setFont(new Font("哥特字体", Font.PLAIN, 15));
        graphics.drawString("￥ " + String.valueOf(totalSumTax), 748, 471);

        //备注invcomment
        graphics.setColor(Color.blue);
        graphics.setFont(new Font("哥特字体", Font.PLAIN, 15));
        String remark = sellerInvoiceEntity.getRemark();
        //graphics.drawString(remark == null ? "" : remark, 510, 405);
        drawStringWithFontStyleLineFeed(graphics,remark == null ? "" : remark,250,711,706,new Font("哥特字体", Font.PLAIN, 12));
        return graphics;
    }

    public static Graphics getITEXT(Graphics graphics, String cipherText) {
        if (!"".equals(cipherText) && cipherText != null) {
            int invpwds = pwdlength(cipherText);
            graphics.setColor(Color.blue);
            graphics.setFont(new Font("哥特字体", Font.PLAIN, 12));
            graphics.drawString(cipherText.substring(0, invpwds), 520, 119);

            graphics.setColor(Color.blue);
            graphics.setFont(new Font("哥特字体", Font.PLAIN, 12));
            graphics.drawString(cipherText.substring(invpwds, 2 * invpwds), 520, 138);

            graphics.setColor(Color.blue);
            graphics.setFont(new Font("哥特字体", Font.PLAIN, 12));
            graphics.drawString(cipherText.substring(2 * invpwds, 3 * invpwds), 520, 157);

            graphics.setColor(Color.blue);
            graphics.setFont(new Font("哥特字体", Font.PLAIN, 12));
            graphics.drawString(cipherText.substring(3 * invpwds, 4 * invpwds), 520, 177);
        }
        return graphics;
    }

    public static Graphics getCarITEXT(Graphics graphics, String cipherText) {
        if (!"".equals(cipherText) && cipherText != null) {
            int invpwds = pwdCarlength(cipherText);
            graphics.setColor(Color.blue);
            graphics.setFont(new Font("哥特字体", Font.PLAIN, 15));
            graphics.drawString(cipherText.substring(0, invpwds), 485, 179);

            graphics.setColor(Color.blue);
            graphics.setFont(new Font("哥特字体", Font.PLAIN, 15));
            graphics.drawString(cipherText.substring(invpwds, 2 * invpwds), 485, 201);

            graphics.setColor(Color.blue);
            graphics.setFont(new Font("哥特字体", Font.PLAIN, 15));
            graphics.drawString(cipherText.substring(2 * invpwds, 3 * invpwds), 485, 224);

            graphics.setColor(Color.blue);
            graphics.setFont(new Font("哥特字体", Font.PLAIN, 15));
            graphics.drawString(cipherText.substring(3 * invpwds, 4 * invpwds), 485, 248);

            graphics.setColor(Color.blue);
            graphics.setFont(new Font("哥特字体", Font.PLAIN, 15));
            graphics.drawString(cipherText.substring(4 * invpwds, 5 * invpwds), 485, 267);
        }
        return graphics;
    }

    public static Graphics getDetail(Graphics graphics, List<SellerInvoiceDetailEntity> sellerInvoiceDetailEntityList) {
        if (sellerInvoiceDetailEntityList.size() > 8) {
            //货物或应税劳务、服务名称
            graphics.setColor(Color.blue);
            graphics.setFont(new Font("哥特字体", Font.PLAIN, 12));
            graphics.drawString("(详见货物清单)", 83, 215);
        } else {
            for (int i = 0; i < sellerInvoiceDetailEntityList.size(); i++) {
                SellerInvoiceDetailEntity sellerInvoiceDetailEntity = sellerInvoiceDetailEntityList.get(i);
                //货物或应税劳务、服务名称
                graphics.setColor(Color.blue);
                graphics.setFont(new Font("哥特字体", Font.PLAIN, 11));
                String spmc = StringUtil.validator(sellerInvoiceDetailEntity.getGoodsName());
                String spjc = "";
                if (!"".equals(spjc)) {
                    spmc = "*" + spjc + "*" + spmc;
                }

                if (spmc.length() > 16) {
                    spmc = spmc.substring(0, 14) + "...";
                }
                graphics.drawString(sellerInvoiceDetailEntity.getGoodsName() == null ? "" : spmc, 73, 215 + 16 * i);

                graphics = getInvoiceDetail(graphics, sellerInvoiceDetailEntity, i);

            }
        }

        return graphics;
    }

    public static Graphics getInvoiceDetail(Graphics graphics, SellerInvoiceDetailEntity sellerInvoiceDetailEntity, int i) {
        //规格型号
        graphics.setColor(Color.blue);
        graphics.setFont(new Font("哥特字体", Font.PLAIN, 10));
        String ggxh = StringUtil.validator(sellerInvoiceDetailEntity.getModel());
        if (ggxh.length() > 8) {
            ggxh = ggxh.substring(0, 7) + "...";
        }
        graphics.drawString(sellerInvoiceDetailEntity.getModel() == null ? "" : ggxh, 235, 215 + 16 * i);

        //单位
        graphics.setColor(Color.blue);
        graphics.setFont(new Font("哥特字体", Font.PLAIN, 12));
        String unit = sellerInvoiceDetailEntity.getUnit();
        graphics.drawString(unit == null ? "" : unit, 320, 215 + 16 * i);

        //数量
        graphics.setColor(Color.blue);
        graphics.setFont(new Font("哥特字体", Font.PLAIN, 12));
        String goodNumber = sellerInvoiceDetailEntity.getGoodsNumber();
        graphics.drawString(goodNumber == null ? "" : goodNumber, 370, 215 + 16 * i);

        //单价
        graphics.setColor(Color.blue);
        graphics.setFont(new Font("哥特字体", Font.PLAIN, 12));
        String unitPrice = sellerInvoiceDetailEntity.getUnitPrice();
        graphics.drawString(unitPrice == null ? "" : unitPrice, 480, 215 + 16 * i);

        //金额
        graphics.setColor(Color.blue);
        graphics.setFont(new Font("哥特字体", Font.PLAIN, 12));
        String goodSum = sellerInvoiceDetailEntity.getGoodsSum();
        graphics.drawString(goodSum == null ? "" : goodSum, 590, 215 + 16 * i);

        //税率
        graphics.setColor(Color.blue);
        graphics.setFont(new Font("哥特字体", Font.PLAIN, 12));
        String taxRate = sellerInvoiceDetailEntity.getTaxRate();

        try {
            if ("".equals(taxRate)) {
                graphics.drawString("", 697, 215 + 16 * i);
            } else {
                boolean checkPercent = isSpecialChar(taxRate);
                if (!checkPercent) {
                    taxRate = String.valueOf((int) (100 * Double.parseDouble(taxRate))) + "%";
                }
                graphics.drawString(taxRate, 697, 215 + 16 * i);
            }
        } catch (Exception e) {
            log.info(e.getMessage());
        }
        //税额
        graphics.setColor(Color.blue);
        graphics.setFont(new Font("哥特字体", Font.PLAIN, 12));
        String goodTax = sellerInvoiceDetailEntity.getGoodTax();
        graphics.drawString(goodTax == null ? "" : goodTax.toString(), 735, 215 + 16 * i);
        return graphics;
    }


    public static String getFplxStr(String invoiceCode, String invoiceType) {
        String res = "增值税专用发票";
        if (StrUtil.isNotEmpty(invoiceCode)) {
            try {
                Map mapArea = AreaCodeUtil.recoginizeArea(invoiceCode);
                String area = mapArea.get("fpssqymc") == null ? "" : mapArea.get("fpssqymc").toString();
                if ("007".equals(invoiceType)) {//此处调用王宁的方法判断
                    res = area + "增值税普通发票";
                } else if ("004".equals(invoiceType)) {
                    res = area + "增值税专用发票";
                } else if ("026".equals(invoiceType)) {
                    res = area + "电子普通发票";
                }
            } catch (Exception e) {
                log.info(e.getMessage());
            }
        } else {
            if ("007".equals(invoiceType)) {//此处调用王宁的方法判断
                res = "增值税普通发票";
            } else if ("004".equals(invoiceType)) {
                res = "增值税专用发票";
            } else if ("026".equals(invoiceType)) {
                res = "电子普通发票";
            }
        }
        return res;
    }


    public static int number(int j, String b) {
        int end = 0;
        int i = b.length();
        end = j - (16 - i) * 6;
        return end;

    }

    public static String transAddLine(String date) {
        String regEx = "-";
        Pattern p = Pattern.compile(regEx);
        Matcher m = p.matcher(date);
        boolean b = m.find();
        if (b) {
            return date;
        }
        return DateUtil.format(DateUtil.parse(date, "yyyyMMdd"), "yyyy-MM-dd");
    }


    public static boolean isSpecialChar(String str) {
        String regEx = "%";
        Pattern p = Pattern.compile(regEx);
        Matcher m = p.matcher(str);
        return m.find();
    }

    public static boolean isSpecialCharCheck(String str, String special) {
        String regEx = special;
        Pattern p = Pattern.compile(regEx);
        Matcher m = p.matcher(str);
        return m.find();
    }


    public static int pwdlength(String invpwd) {
        int pwd4L = invpwd.length();
        int pwdL = pwd4L / 4;
        return pwdL;


    }

    public static int pwdCarlength(String invpwd) {
        int pwd4L = invpwd.length();
        int pwdL = pwd4L / 5;
        return pwdL;


    }

}    
 
  